// pages/market/online-experience/online-experience.js
const app = getApp()
var store = require("../../../utils/storageUtil.js")
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")
const util = require("../../../utils/util.js")

import WxValidate from '../../../utils/WxValidate'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    qiniu: constant.resource.qiniu
  },
  initValidate: function () {
    let that = this;
    // 验证字段的规则
    const rules = {
      name: {
        required: true,
      },
      // wechat: {
      //   required: true,
      //   wechat: true
      // },
      height: {
        required: true,
        number: true,
      },
      weight: {
        required: true,
        number: true,
      },
      // bust: {
      //   required: true,
      //   number: true,
      // },
      // waist: {
      //   required: true,
      //   number: true,
      // },
      // hips: {
      //   required: true,
      //   number: true,
      // },
      fitness_frequency: {
        required: true,
      },
      disabled: false
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      name: {
        required: '请输入姓名',
      },
      // wechat: {
      //   required: '请输入微信号',
      // },
      height: {
        required: '请输入身高',
        number: '请输入正确的身高',
      },
      weight: {
        required: '请输入体重',
        number: '请输入正确的体重',
      },
      // bust: {
      //   required: '请输入胸围',
      //   number: '请输入正确的胸围',
      // },
      // waist: {
      //   required: '请输入腰围',
      //   number: '请输入正确的腰围',
      // },
      // hips: {
      //   required: '请输入臀围',
      //   number: '请输入正确的臀围',
      // },
      fitness_frequency: {
        required: '请选择运动强度',
      }
    }

    that.WxValidate = new WxValidate(rules, messages)
    
    // that.WxValidate.addMethod('wechat', (value, param) => {
    //   return that.WxValidate.optional(value) || (/^['?!=%;&\$a-zA-Z\d_-]{6,20}$/.test(value))
    // }, '请输入正确的微信号（不是微信昵称）')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url
    })

    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  formSubmit: function (e) {
    var that = this;
    that.setData({
      disabled: true
    })
    var data = e.detail.value;
    data.wechat = '0';
    data.bust= '0';
    data.waist= '0';
    data.hips='0';

    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 1000
      })
      that.setData({
        disabled: false
      })
      return false
    } else {
      if (that.data.mobile && !/^((13)|(15)|(16)|(17)|(18))\d{9}$/.test(that.data.mobile)) {
        wx.showToast({
          title: '请输入正确的手机号码',
          icon: 'none',
          duration: 1000
        })
        that.setData({
          disabled: false
        })
        return false;
      }

      wx.request({
        url: that.data.host + '/experience-sign-up/online',
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            that.setData({
              showModal: true
            })
          } else {
            that.setData({
              disabled: false
            })
            wx.showToast({
              title: res.data.message ? res.data.message : '预约失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
          }
        },
        fail: function (res) {
          that.setData({
            disabled: false
          })
          wx.showToast({
            title: '预约线上体验失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      });

    }


  },

  inputMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  }
})